<template>
	<view>
		<view class="default" v-if="isIframe && !couponList.length">
			<text>红包券，暂无数据</text>
		</view>
		<view class="coupon" v-if="couponList.length&&isShow&&!isIframe">
			<view class="title acea-row row-between-wrapper">
				<view class="acea-row row-middle">
					<view class="sign">
						<image src="../../../static/images/sign01.png"></image>
					</view>
					<view class="name">领专享<text>红包券</text></view>
				</view>
				<navigator url="/pages/users/user_get_coupon/index" hover-class="none" class="more acea-row row-center-wrapper">更多<text
					 class="iconfont icon-xiangyou"></text></navigator>
			</view>
			<view class="conter">
				<scroll-view scroll-x="true" style="white-space: nowrap; vertical-align: middle;" show-scrollbar="false">
					<view class="itemCon" :class="item.is_use?'on':''" v-for="(item, index) in couponList" :key="index">
						<view class="item acea-row row-between-wrapper">
							<view class="text">
								<view class="money line1">¥<text class="num">{{item.coupon_price}}</text></view>
								<view class="line1">满{{item.use_min_price}}可用</view>
							</view>
							<view class="bnt" v-if="item.is_use===true"><text>已领取</text></view>
							<view class="bnt" v-else-if="item.is_use===false" @click="receiveCoupon(item)"><text>领取</text></view>
							<view class="bnt" v-else-if="item.is_use===2"><text>已过期</text></view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="coupon" v-if="couponList.length && isIframe">
			<view class="title acea-row row-between-wrapper">
				<view class="acea-row row-middle">
					<view class="sign">
						<image src="../../../static/images/sign01.png"></image>
					</view>
					<view class="name">领专享<text>红包券</text></view>
				</view>
				<navigator url="/pages/users/user_get_coupon/index" hover-class="none" class="more acea-row row-center-wrapper">更多<text
					 class="iconfont icon-xiangyou"></text></navigator>
			</view>
			<view class="conter">
				<scroll-view scroll-x="true" style="white-space: nowrap; vertical-align: middle;" show-scrollbar="false">
					<view class="itemCon" :class="item.is_use?'on':''" v-for="(item, index) in couponList" :key="index">
						<view class="item acea-row row-between-wrapper">
							<view class="text">
								<view class="money line1">¥<text class="num">{{item.coupon_price}}</text></view>
								<view class="line1">满{{item.use_min_price}}可用</view>
							</view>
							<view class="bnt" v-if="item.is_use===true"><text>已领取</text></view>
							<view class="bnt" v-else-if="item.is_use===false"><text>领取</text></view>
							<view class="bnt" v-else-if="item.is_use===2"><text>已过期</text></view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	let app = getApp();
	import {
		getIndexCoupons,
		setCouponReceive
	} from '@/api/api.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import { mapGetters } from 'vuex';
	export default {
		name: 'coupon',
		props: {
			dataConfig: {
				type: Object,
				default: () => {}
			}
		},
		computed: {
			 ...mapGetters(['isLogin'])
		},
		watch: {
			dataConfig: {
				immediate: true,
				handler(nVal, oVal) {
					if(nVal){
						this.numberConfig = nVal.numConfig.val;
						this.isShow = nVal.isShow.val;
						this.getCoupon();
					}
				}
			}
		},
		data() {
			return {
				couponList: [],
				numberConfig:0,
				name:this.$options.name,//component组件固定写法获取当前name；
				isIframe:app.globalData.isIframe,//判断是前台还是后台；
				isShow:true//判断此模块是否显示；
			};
		},
		created() {},
		mounted() {},
		methods: {
			getCoupon: function() {
				let that = this;
				let lists = [];
				getIndexCoupons({type:-1,num:this.numberConfig}).then(res => {
					res.data.forEach(function(value, key, iterable) {
					  if(!value.used){
						 lists.push(value);
					  }
					});
					that.$set(that, 'couponList', lists);
				}).catch(err => {
					return that.$util.Tips({
						title: err
					});
				});
			},
			receiveCoupon: function(item) {
				let that = this;
				if (!that.isLogin) {
					toLogin();
				} else {
					setCouponReceive(item.id)
						.then(function() {
							item.is_use = true;
							that.$set(that, 'couponList', that.couponList);
							that.$util.Tips({
								title: "领取成功"
							});
						})
						.catch(function(err) {
							that.$util.Tips({
								title: err
							});
						});
				}
			}
		}
	}
</script>

<style lang="scss">
	.default{
		width: 690rpx;
		height: 300rpx;
		border-radius: 14rpx;
		margin: 26rpx auto 0 auto;
		background-color: #ccc;
		text-align: center;
		line-height: 300rpx;
		.iconfont{
			font-size: 50rpx;
		}
	}
	.coupon {
		width: 690rpx;
		height: 230rpx;
		background: url('') no-repeat;
		background-size: 100% 100%;
		margin: 26rpx auto 0 auto;
		padding: 25rpx 0 32rpx 0;

		.title {
			padding: 0 20rpx;

			.sign {
				width: 40rpx;
				height: 40rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.name {
				font-size: 32rpx;
				color: #9A6314;
				margin-left: 12rpx;
				font-weight: bold;

				text {
					color: #FD502F;
				}
			}

			.more {
				width: 86rpx;
				height: 36rpx;
				background: linear-gradient(90deg, #FF9F6C 0%, #FD502F 100%);
				border-radius: 21rpx;
				font-size: 22rpx;
				color: #fff;
				padding-left: 8rpx;

				.iconfont {
					font-size: 21rpx;
				}
			}
		}

		.conter {
			margin-top: 25rpx;
			padding-left: 20rpx;

			.itemCon {
				background: url("") no-repeat;
				background-size: 100% 100%;
				width: 226rpx;
				height: 108rpx;
				display: inline-block;
				margin-right: 14rpx;

				&.on {
					background-image: url("");

					.item {
						.text {
							color: #BFBFBF;
						}
					}
				}

				.item {
					width: 100%;
					height: 100%;

					.text {
						text-align: center;
						color: #FD502F;
						font-size: 22rpx;
						width: 168rpx;
						padding-bottom: 6rpx;

						.money {
							font-size: 30rpx;
							font-weight: bold;

							.num {
								font-size: 46rpx;
							}
						}
					}

					.bnt {
						writing-mode: vertical-lr;
						font-size: 22rpx;
						color: #fff;
						margin: 0 auto;
					}
				}
			}
		}
	}
</style>